package cn.xz.mxxz.base

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import cn.xz.mxxz.bean.EventBusMessage
import cn.xz.mxxz.util.EventBusUtils
import org.greenrobot.eventbus.Subscribe

abstract class BaseActivity<T : ViewDataBinding> (
    private var layoutId: Int,
    private var setEventBus: Boolean = false //是否注册EventBus,默认不注册
): AppCompatActivity() {

    lateinit var mDataBind: T

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mDataBind = DataBindingUtil.setContentView(this, layoutId)
        initData()
        initEvent()
        if (setEventBus){
            EventBusUtils.register(this)
        }
    }

    override fun onResume() {
        super.onResume()
        hideBottomUIMenu()
    }

    open fun initData(){}

    open fun initEvent(){}

    open fun initEventBus(message: EventBusMessage){}

    //隐藏导航栏
    private fun hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            val v = this.window.decorView
            v.systemUiVisibility = View.GONE
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            val decorView = window.decorView
            val uiOptions = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN)
            decorView.systemUiVisibility = uiOptions
        }
    }

    @Subscribe
    fun onSubscriberEvent(message: EventBusMessage) {
        initEventBus(message)
    }

    override fun onDestroy() {
        super.onDestroy()
        if (setEventBus){
            EventBusUtils.unRegister(this)
        }
    }
}